package com.str.config;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.str.dto.ImportKitInfoDto;
import com.str.dto.ImportLocusInfoDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.StringJoiner;

/**
 * @Author: ZhangWu
 * @DateTime: 2022/9/23 16:11
 * @Description: 风险清单数据参数自定义校验器
 */
@Component
public class TalentImportVerifyHandler implements IExcelVerifyHandler<ImportKitInfoDto> {

    @Override
    public ExcelVerifyHandlerResult verifyHandler(ImportKitInfoDto obj) {
        StringJoiner joiner = new StringJoiner(",");
        if (Objects.nonNull(obj)){
            //校验限制字符串长度是否超标
            if (StringUtils.isBlank(obj.getKitName())){
                joiner.add(String.format("试剂盒名称为空，导入失败！"));
            }
            if (Objects.isNull(obj.getComNumber())){
                joiner.add(String.format("组分数为空，导入失败！"));
            }
            if (Objects.nonNull(obj.getComNumber())){
                if (obj.getComNumber()!=0 && obj.getComNumber()!=1 && obj.getComNumber()!=2 && obj.getComNumber()!=3 && obj.getComNumber()!=4){
                    joiner.add(String.format("组分数填充错误，导入失败！"));
                }
            }
            if (StringUtils.isBlank(obj.getSource())){
                joiner.add(String.format("来源为空，导入失败！"));
            }
            if (Objects.nonNull(obj.getList()) && obj.getList().size()>0){
                for (ImportLocusInfoDto controlMeasuresVO : obj.getList()) {
                    if (StringUtils.isBlank(controlMeasuresVO.getLocusName())){
                        joiner.add(String.format("基因座为空，导入失败！"));
                    }
                    if (StringUtils.isBlank(controlMeasuresVO.getGenotypeOne())){
                        joiner.add(String.format("个体示例数据1为空，导入失败！"));
                    }
                    if (StringUtils.isBlank(controlMeasuresVO.getGenotypeTwo())){
                        joiner.add(String.format("个体示例数据2为空，导入失败！"));
                    }
                    if (StringUtils.isBlank(controlMeasuresVO.getAllele())){
                        joiner.add(String.format("等位基因为空，导入失败！"));
                    }
                    if (StringUtils.isBlank(controlMeasuresVO.getPeakArea())){
                        joiner.add(String.format("峰面积为空，导入失败！"));
                    }
                }
            }else {
                joiner.add(String.format("基因座信息为空，导入失败！"));
            }
        }
        if (joiner.length() != 0) {
            return new ExcelVerifyHandlerResult(false, joiner.toString());
        }
        return new ExcelVerifyHandlerResult(true);
    }
}
